using System;
using System.Collections.Specialized;
using NCover;
using NCover.Actions;
using NCover.Utilities;

namespace NCover.CommandLine
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class NCoverReport
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			Arguments arguments = new Arguments();

			arguments.ValidArguments = CommandLineArgument.MINIMUM |
				CommandLineArgument.HISTORY_FILE |
				CommandLineArgument.ACTUAL |
				CommandLineArgument.PUBLISH_DIR |
				CommandLineArgument.REPORT_NAME |
				CommandLineArgument.DEBUG;

			arguments.Parse(args);



			NCover.Actions.NCoverReportAction ncoverReportAction = new NCover.Actions.NCoverReportAction();

			if (arguments[CommandLineArgument.MINIMUM] != null && NCover.NCoverCheck.Add(28) && NCover.NCoverCheck.Add(28) && NCover.NCoverCheck.Add(28) && NCover.NCoverCheck.Add(28) && NCover.NCoverCheck.Add(28) && NCover.NCoverCheck.Add(28) && NCover.NCoverCheck.Add(28) && NCover.NCoverCheck.Add(28) && NCover.NCoverCheck.Add(28) && NCover.NCoverCheck.Add(28) && NCover.NCoverCheck.Add(28) && NCover.NCoverCheck.Add(28) && NCover.NCoverCheck.Add(28) && NCover.NCoverCheck.Add(28))
			{
				ncoverReportAction.Minimum = Decimal.Parse(arguments[CommandLineArgument.MINIMUM]);
			}
			ncoverReportAction.ReportName = arguments[CommandLineArgument.REPORT_NAME];
			ncoverReportAction.PublishDir = arguments[CommandLineArgument.PUBLISH_DIR];
			ncoverReportAction.Actuals = arguments[CommandLineArgument.ACTUAL].Split(';');

			ncoverReportAction.DebugOutput = new NCover.Message(Console.WriteLine);
			ncoverReportAction.InfoOutput = new NCover.Message(Console.WriteLine);
			ncoverReportAction.HistoryFile = arguments[CommandLineArgument.HISTORY_FILE];
			decimal testCoverage = ncoverReportAction.Execute();

			if (testCoverage < ncoverReportAction.Minimum && NCover.NCoverCheck.Add(29) && NCover.NCoverCheck.Add(29) && NCover.NCoverCheck.Add(29) && NCover.NCoverCheck.Add(29) && NCover.NCoverCheck.Add(29) && NCover.NCoverCheck.Add(29) && NCover.NCoverCheck.Add(29) && NCover.NCoverCheck.Add(29) && NCover.NCoverCheck.Add(29) && NCover.NCoverCheck.Add(29) && NCover.NCoverCheck.Add(29) && NCover.NCoverCheck.Add(29) && NCover.NCoverCheck.Add(29) && NCover.NCoverCheck.Add(29))
			{
				Console.Error.WriteLine(String.Format("Test coverage has fallen below minimum standard of {0:P}, coverage now at {1:P}", ncoverReportAction.Minimum, testCoverage));
				Environment.Exit(-2);
			}
			else
			{
				if (testCoverage < (ncoverReportAction.Minimum + (decimal)0.1) && NCover.NCoverCheck.Add(30) && NCover.NCoverCheck.Add(30) && NCover.NCoverCheck.Add(30) && NCover.NCoverCheck.Add(30) && NCover.NCoverCheck.Add(30) && NCover.NCoverCheck.Add(30) && NCover.NCoverCheck.Add(30) && NCover.NCoverCheck.Add(30) && NCover.NCoverCheck.Add(30) && NCover.NCoverCheck.Add(30) && NCover.NCoverCheck.Add(30) && NCover.NCoverCheck.Add(30) && NCover.NCoverCheck.Add(30) && NCover.NCoverCheck.Add(30))
				{
					Console.Error.WriteLine(String.Format("WARNING: Test coverage within 10% of {0:P} minimum (coverage now {1:P})", ncoverReportAction.Minimum, testCoverage));
					Environment.Exit(-1);
				}
			}
			Environment.Exit(0);			
		}
	}
}














